 //
//  DDOrderManager.m
//  Dingdong
//
//  Created by 程强 on 16/11/26.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDOrderManager.h"
#import "DDOrderSet.h"
#import "DDReceiptAddSet.h"
#import "DDPrepareOrderSet.h"
#import "DDCouponSet.h"

@implementation DDOrderManager
- (DSASubject *)getOrderList:(NSString *)status pageNumber:(NSInteger)pageNo pageSize:(NSInteger)size
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/order/getOrderPage.do", ServerUrl];
    
    NSDictionary *body = @{@"status":status,@"pageNo":@(pageNo),@"pageSize":@(size)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDOrderSet *set = [DDOrderSet modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)prepareForCommitOrderInfo:(NSNumber *)add_id isCountShip:(NSString *)isCountShip bonusJson:(NSString *)bonusJson
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/order/checkout.do", ServerUrl];
    
    NSDictionary *body = @{@"addrId":add_id,@"isCountShip":isCountShip,@"bonusJson":bonusJson};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDPrepareOrderSet *set = [DDPrepareOrderSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getGiftInfo:(NSNumber *)gift_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/gift/get.do", ServerUrl];
    
    NSDictionary *body = @{@"giftId":gift_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDPrepareOrderGift *set = [DDPrepareOrderGift modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getCoupons:(NSNumber *)store_id storeprice:(NSString *)storeprice
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/bonus/listUsable.do", ServerUrl];
    
    NSDictionary *body = @{@"store_id":store_id,@"storeprice":storeprice};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCouponSet *set = [DDCouponSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getDefaultAddress
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/address/getdef.do", ServerUrl];
    
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDReceiptAddObj *set = [DDReceiptAddObj modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)createOrderWithAddress:(NSNumber *)addrId shipDay:(NSString *)shipDay shipTime:(NSString *)shipTime bonusJson:(NSString *)bonusJson paymentId:(NSString *)paymentId remark:(NSString *)remark
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/order/create.do", ServerUrl];
    
    NSDictionary *body = @{@"addrId":addrId,@"shipDay":shipDay,@"shipTime":shipTime,@"bonusJson":bonusJson,@"paymentId":paymentId,@"remark":remark};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDReceiptAddObj *set = [DDReceiptAddObj modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)confirmOrder:(NSNumber *)order_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/orderDetail/rogConfirm.do", ServerUrl];
    
    NSDictionary *body = @{@"order_id":order_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)deleteOrder:(NSNumber *)order_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/orderDetail/delOrder.do", ServerUrl];
    
    NSDictionary *body = @{@"order_id":order_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDReceiptAddObj *set = [DDReceiptAddObj modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)cancelOrder:(NSNumber *)order_id reason:(NSString *)reason
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/orderDetail/cancelOrder.do", ServerUrl];
    
    NSDictionary *body = @{@"order_id":order_id,@"reason":reason};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDReceiptAddObj *set = [DDReceiptAddObj modelWithDictionary:rdic[@"data"]];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
@end
